/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "serial.h"
#include "calibration.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
typedef enum
{
	kLogNone,
	kLogError,
	kLogWarn,
	kLogInfo,
} log_level_t;

typedef enum
{
    kOFF,
    kON,
} switch_state_t;

typedef enum
{
    kOK,
    kERR,
    kRESTART,
} error_state_t;

typedef enum
{
    kModule1st,
    kModule2nd,
    kModule3rd,
    kModule4th,
    kModule5th,
    kModule6th,
    kModule7th,

    kModuleNum
} pa_module_id_t;

typedef enum
{
    kSubCtrl1st,
    kSubCtrl2nd,
    kSubCtrl3rd,
    kSubCtrl4th,
    kSubCtrl5th,
    kSubCtrl6th,

    kSubCtrlNum
} sub_control_id_t;

typedef struct
{
    GPIO_TypeDef *port;
    uint16_t pin;
    float *val_ptr;
} ds18b20_config_t;

typedef struct
{
    GPIO_TypeDef *port;
    uint16_t pin;
} pd_pin_index_t;

typedef struct
{
    GPIO_TypeDef *port;
    uint16_t pin;
} phase_nss_index_t;

typedef struct
{
    const ds18b20_config_t *temp_index;
    const pd_pin_index_t *pd_index;
} pa_module_hardware_config_t;

typedef struct
{
    float current;
    float temperature;
} pa_module_t;

typedef struct
{
    uint8_t pa_enable : 7;
    switch_state_t auto_run : 1;
    error_state_t error : 4;
    uint8_t soft_reset_count : 8;
} sub_control_state_t;

typedef struct
{
    cubic_param_t forward;  // 正向校准参数
} sub_config_t;
/**
 * @brief 分控制器flash数据结构体
 *
 */
typedef struct
{
    uint8_t init;
    uint8_t ip[4];
    sub_config_t config;
    /*待定*/
} __ALIGNED(4) sub_control_flash_t;
/**
 * @brief 分控制器结构体
 *
 */
typedef struct
{
    uint8_t frame_header;      // 帧头
    struct
    {
        uint8_t type :4;         // 控制器类型
        uint8_t init :4;         // 初始化标志
        uint8_t ip[4];           // IP地址
    } header;
    sub_control_state_t state; // 状态
    pa_module_t pa[kModuleNum]; // PA模块
    struct
    {
        int target;
        float forward;
        float reverse;
    } rf_power;                // 射频功率
    union {
        uint8_t byte[4];
        uint16_t half[2];
        uint32_t word;
    } check;
} sub_control_t;

typedef enum{
    kSerialCmdQuery,
    kSerialCmdReset,
    kSerialCmdSetPower,
    kSerialCmdSetAddress,
    kSerialCmdSaveParameter,
    kSerialCmdSetForwardPoint,
    kSerialCmdSetForwardCalibration,
    kSerialCmdGetForwardCalibration,
    kSerialCmdStartForwardCalibration,
    kSerialCmdResetForwardCalibration,
    kSerialCmdSetMaxForwardPower,
} serial_cmd_t;

typedef enum
{
    kRS485CmdQuery,
    kRS485CmdSaveParameter,
    kRS485CmdSetPower,
} rs485_cmd_t;

extern log_level_t log_level;
extern serial_handle_t log_handle;
extern serial_handle_t rs485_handle;
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define PD2_Pin GPIO_PIN_13
#define PD2_GPIO_Port GPIOC
#define PD1_Pin GPIO_PIN_14
#define PD1_GPIO_Port GPIOC
#define TEMP4_Pin GPIO_PIN_15
#define TEMP4_GPIO_Port GPIOC
#define CURRENT1_Pin GPIO_PIN_0
#define CURRENT1_GPIO_Port GPIOA
#define CURRENT2_Pin GPIO_PIN_1
#define CURRENT2_GPIO_Port GPIOA
#define CURRENT3_Pin GPIO_PIN_2
#define CURRENT3_GPIO_Port GPIOA
#define CURRENT4_Pin GPIO_PIN_3
#define CURRENT4_GPIO_Port GPIOA
#define CURRENT5_Pin GPIO_PIN_4
#define CURRENT5_GPIO_Port GPIOA
#define CURRENT6_Pin GPIO_PIN_5
#define CURRENT6_GPIO_Port GPIOA
#define CURRENT7_Pin GPIO_PIN_6
#define CURRENT7_GPIO_Port GPIOA
#define RF_JB1_Pin GPIO_PIN_7
#define RF_JB1_GPIO_Port GPIOA
#define RF_JB2_Pin GPIO_PIN_0
#define RF_JB2_GPIO_Port GPIOB
#define TEMP5_Pin GPIO_PIN_1
#define TEMP5_GPIO_Port GPIOB
#define LED5_Pin GPIO_PIN_12
#define LED5_GPIO_Port GPIOB
#define LED4_Pin GPIO_PIN_13
#define LED4_GPIO_Port GPIOB
#define LED3_Pin GPIO_PIN_14
#define LED3_GPIO_Port GPIOB
#define LED2_Pin GPIO_PIN_15
#define LED2_GPIO_Port GPIOB
#define LED1_Pin GPIO_PIN_8
#define LED1_GPIO_Port GPIOA
#define TEMP3_Pin GPIO_PIN_11
#define TEMP3_GPIO_Port GPIOA
#define TEMP2_Pin GPIO_PIN_12
#define TEMP2_GPIO_Port GPIOA
#define TEMP1_Pin GPIO_PIN_15
#define TEMP1_GPIO_Port GPIOA
#define TEMP6_Pin GPIO_PIN_3
#define TEMP6_GPIO_Port GPIOB
#define TEMP7_Pin GPIO_PIN_4
#define TEMP7_GPIO_Port GPIOB
#define PD7_Pin GPIO_PIN_5
#define PD7_GPIO_Port GPIOB
#define PD6_Pin GPIO_PIN_6
#define PD6_GPIO_Port GPIOB
#define PD5_Pin GPIO_PIN_7
#define PD5_GPIO_Port GPIOB
#define PD4_Pin GPIO_PIN_8
#define PD4_GPIO_Port GPIOB
#define PD3_Pin GPIO_PIN_9
#define PD3_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */

#define LED_ON() HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET)
#define LED_OFF() HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET)
#define LED_TOGGLE() HAL_GPIO_TogglePin(LED5_GPIO_Port, LED5_Pin)
#define LED_TEMP_ON() HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_RESET)
#define LED_TEMP_OFF() HAL_GPIO_WritePin(LED5_GPIO_Port, LED5_Pin, GPIO_PIN_SET)
#define LED_CURRENT_ON() HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_RESET)
#define LED_CURRENT_OFF() HAL_GPIO_WritePin(LED4_GPIO_Port, LED4_Pin, GPIO_PIN_SET)
#define LED_WARE_ON() HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_RESET)
#define LED_WARE_OFF() HAL_GPIO_WritePin(LED3_GPIO_Port, LED3_Pin, GPIO_PIN_SET)
#define LED_COMM_ON() HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_RESET)
#define LED_COMM_OFF() HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET)
#define LED_OUTPUT_ON() HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET)
#define LED_OUTPUT_OFF() HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET)

#define SERIAL_FRAME_HEADER 0x35

#define RS485_FRAME_HEADER 0x34
#define SUBCTRL_BASE_ADDRESS 101
// #define RS485_UNIVERSAL_ADDRESS 233
#define RS485_TYPE_QUERY 0x00
#define RS485_TYPE_SAVE_PARAMETER 0x01
#define RS485_TYPE_POWER 0x02
// #define RS485_TYPE_PHASE 0x03
// #define RS485_TYPE_ATTENUATION 0x04
// #define RS485_TYPE_AUTO_RUN 0x05
// #define RS485_TYPE_FORWAR_PID 0x06
// #define RS485_TYPE_REVERSE_PID 0x07

#define UART_HANDLE_LOG huart1
#define UART_HANDLE_RS485 huart3

#define ENABLE_DEBUG_MODE 0
#define DEFAULT_INITIAL_VALUE 0x01

#define SAMPLING_RESISTOR 0.005f
#define MAX4080_GAIN 60

#define LOG_ENABLE  1
#if LOG_ENABLE
#define LOGI(format, ...) if(log_level >= kLogInfo) Serial_Printf(log_handle, "Info: " format "\n", ##__VA_ARGS__)
#define LOGW(format, ...) if(log_level >= kLogWarn) Serial_Printf(log_handle, "Warn: " format "\n", ##__VA_ARGS__)
#define LOGE(format, ...) if(log_level >= kLogError) Serial_Printf(log_handle, "Error: " format "\n", ##__VA_ARGS__)
#else
#define LOGI(format, ...)
#define LOGW(format, ...)
#define LOGE(format, ...)
#endif
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
